// trainShare.js
var screenCapturePath = '';//截图路径，默认空字符串
var currentUrl = '';
//截屏
function onScreenCapture(elId, callback){
	if (screenCapturePath != '') {
		if (callback != null) {
    		callback(true, screenCapturePath);
    		return;
    	}
	}
	api.showProgress({
	    style: 'default',
	    animationType: 'fade',
	    title: '',
	    text: '生成页面截图中...',
	    modal: true
	});
	html2canvas(document.getElementById(elId), {
		useCORS: true,
	    logging: false
	}).then(function(canvas) {
        var dataUrl = canvas.toDataURL();
        saveBase64ImageToCache(dataUrl, function(status, imagePath){
        	if (callback != null) {
        		callback(status, imagePath);
        	}
        });
    });
}
function createShareImage(elId, callback){
	if (screenCapturePath != '') {
		if (callback != null) {
    		callback(true, screenCapturePath);
    		return;
    	}
	}
	api.showProgress({
	    style: 'default',
	    animationType: 'fade',
	    title: '',
	    text: '生成分享图片中...',
	    modal: true
	});
	html2canvas(document.getElementById(elId), {
		useCORS: true,
	    logging: false
	}).then(function(canvas) {
        var dataUrl = canvas.toDataURL();
        saveBase64ImageToCache(dataUrl, function(status, imagePath){
        	if (callback != null) {
        		callback(status, imagePath);
        	}
        });
    });
}
//保存base64图片
function saveBase64ImageToCache(base64String, callback){
	base64String = base64String.replace('data:image/png;base64,', '');
	var trans = api.require('trans');
	var imgPath = api.cacheDir + "/tempimg/";
	trans.saveImage({
	    base64Str: base64String,
	    // album: true,
	    imgPath: imgPath,
	    imgName: "shareTemp.png"
	}, function(ret, err) {
		api.hideProgress();
	    if (ret.status) {
	    	var newImagePath = imgPath + 'shareTemp.png';
	    	 if (callback != null) {
		    	callback(ret.status, newImagePath);
		    }
	    }
	});
}
//设置截图
function setupScreenCapture(index){
	if (screenCapturePath != '') {
		setupShare(index);
	}else{
		onScreenCapture('current_page', function(status, imagePath){
			if (status) {
				createShareImage('share_img_div', function(status2, imgPath2){
					screenCapturePath = imgPath2;
					setupShare(index);
				});
			}else{
				api.toast({
				    msg: '截屏失败，请重试！'
				});
			}
		})
	}
}
//设置分享
function setupShare(index){
	switch(index){
		case 0://微信
			shareWeChat('session');
			break;
		case 1://朋友圈
			shareWeChat('timeline');
			break;
		case 2://Q好友
			shareQQ();
			break;
		case 3://Q空间
			shareQQ();
			break;
		case 4://新浪微博
			shareSina();
			break;
		case 5://复制链接
			copyUrl();
			break;
		case 6://保存图片
			saveImage();
			break;
		default:
			break;
	}
	onCancelClick();
}
function copyUrl(){
	var clipBoard = api.require('clipBoard');
	clipBoard.set({
	    value: currentUrl
	}, function(ret, err) {
	    if (ret) {
	    	$(".shareshade").hide();
			layer.msg('链接已复制!',{icon: 'link',time: 2500,area: '5rem',shade : [0.3 , '#000' , true]});
	    }
	});
}
//分享到微信QQ
function shareQQ(type){
	var qq = api.require('qq');
	qq.shareImage({
	    title: '',
	    type : 'QFriend',
	    description: '',
	    imgPath: screenCapturePath
	}, function(ret, err) {
	    if (ret.status) {
	    	api.toast({
	    	    msg: '分享图片成功'
	    	});
	    }else{
	    	api.toast({
	    	    msg: '分享图片失败'
	    	});
	    }
	});
}
//分享到微信
function shareWeChat(type){
	var wx = api.require('wx');
	wx.shareImage({
	    apiKey: '',
	    scene: type,
	    thumb: '',
	    contentUrl: screenCapturePath
	}, function(ret, err) {
	    if (ret.status) {
	    	api.toast({
	    	    msg: '分享图片成功'
	    	});
	    }else{
	    	api.toast({
	    	    msg: '分享图片失败'
	    	});
	    }
	});
}
//分享到新浪微博
function shareSina(){
	var weiboPlus= api.require('weiboPlus');
	weiboPlus.shareImage({
	    text: '',
	    imageUrl: screenCapturePath
	}, function(ret, err) {
	    if (ret.status) {
	    	api.toast({
	    	    msg: '分享图片成功'
	    	});
	    }else{
	    	api.toast({
	    	    msg: '分享图片失败'
	    	});
	    }
	});
}
//保存图片
function saveImage(){
	api.saveMediaToAlbum({
	    path: screenCapturePath
	}, function(ret, err) {
		if (ret) {
			$(".shareshade").hide();
			layer.msg('已保存到相册',{icon: 'down',time: 2500,area: '5rem',shade : [0.3 , '#000' , true]});
		}
	});
}